package com.sunday.scoremanager.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.adapter.CommenAdapter;
import com.sunday.common.widgets.CircleImageView;
import com.sunday.common.widgets.MyRatingBar;
import com.sunday.common.widgets.NoScrollGridView;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.entity.Comment;
import com.sunday.scoremanager.entity.Product;
import com.sunday.scoremanager.ui.ShowBigImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.sunday.scoremanager.base.Constants.TEST_URL;

/**
 * Created by 刘涛 on 2017/10/27.
 */

public class CommentAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private List<Comment> dataSet;
    private LayoutInflater layoutInflater;
    private View.OnClickListener onClickListener;

    public CommentAdapter(Context context, List<Comment> datas) {
        this.mContext = context;
        this.dataSet = datas;
        this.layoutInflater = LayoutInflater.from(mContext);

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.list_comment_item, null);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ListHolder listHolder = (ListHolder) holder;
        final Comment item = dataSet.get(position);
        Glide.with(mContext)
                .load(item.getLogo())
                .into(listHolder.userLogo);
        listHolder.userName.setText(item.getNickName());
        listHolder.ratingBar.setRating((int) item.getScore(), R.drawable.rating_bar, false, 30);
        listHolder.userContent.setText(item.getContent());
        listHolder.commentTime.setText(item.getCreateTime());
        if (item.getImages() != null && !item.getImages().isEmpty()) {
            listHolder.gridView.setAdapter(new CommenAdapter<String>(mContext, item.getImages(), R.layout.list_comment_img_item) {
                @Override
                public void convert(com.sunday.common.adapter.ViewHolder helper, String item, int position) {
                    ImageView imgView = helper.getView(R.id.comment_img);
                    Glide.with(mContext)
                            .load(item)
                            .error(R.mipmap.ic_default)
                            .into(imgView);
                }
            });
            listHolder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(mContext, ShowBigImage.class);
                    intent.putStringArrayListExtra("data", (ArrayList<String>) item.getImages());
                    intent.putExtra("index", position);
                    mContext.startActivity(intent);
                }
            });
        }
        if (item.getReplay() != null && !item.getReplay().isEmpty()) {
            listHolder.llReply.setVisibility(View.VISIBLE);
            listHolder.replyContent.setText(item.getReplay().get(0).getContent());
        } else {
            listHolder.llReply.setVisibility(View.GONE);
        }
        listHolder.rlLayout.setTag(item);
        listHolder.rlLayout.setOnClickListener(onClickListener);

    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    class ListHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.user_logo)
        CircleImageView userLogo;
        @Bind(R.id.user_name)
        TextView userName;
        @Bind(R.id.rating_bar)
        MyRatingBar ratingBar;
        @Bind(R.id.user_content)
        TextView userContent;
        @Bind(R.id.btn_check_more)
        TextView btnCheckMore;
        @Bind(R.id.grid_view)
        NoScrollGridView gridView;
        @Bind(R.id.comment_time)
        TextView commentTime;
        @Bind(R.id.rl_layout)
        RelativeLayout rlLayout;
        @Bind(R.id.ll_reply)
        LinearLayout llReply;
        @Bind(R.id.reply_content)
        TextView replyContent;

        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
